home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / recio202.zip / rputs.c < prev    next >
C/C++ Source or Header  |  1994-05-05  |  4KB  |  111 lines

  1. /*****************************************************************************
  2.    MODULE: rputs.c
  3.   PURPOSE: recio character delimited string and char output functions
  4. COPYRIGHT: (C) 1994 William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.02
  8.   RELEASE: May 5, 1994
  9. *****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include "recio.h"
  16.  
  17. extern int _rstatus(REC *rp, int mode);
  18. extern int _rputc(REC *rp, int ch);
  19.  
  20. #define rflags(rp)       ((rp)->r_flags)
  21. #define rfp(rp)          ((rp)->r_fp)
  22. #define rcol(rp)         ((rp)->r_colno)
  23. #define rfldch(rp)       ((rp)->r_fldch)
  24. #define rtxtch(rp)       ((rp)->r_txtch)
  25.  
  26. /****************************************************************************/
  27. int                          /* return 0 on success; !0 on error            */
  28.     _rputc(                  /* put character to record stream              */
  29.         REC *rp,             /* record pointer                              */
  30.         int  ch)             /* character to put to record stream           */
  31. /****************************************************************************/
  32. {
  33.     int err = fputc(ch, rfp(rp));
  34.     if (err==EOF) {
  35.         rseterr(rp, R_ENOPUT);
  36.     } else {
  37.         rcol(rp)++;
  38.         err = 0;
  39.     }
  40.     return err;
  41. }
  42.  
  43. /****************************************************************************/
  44. int                          /* return 0 on success; !0 on error            */
  45.     rputc(                   /* put character to record stream              */
  46.         REC *rp,             /* record pointer                              */
  47.         int  ch)             /* character to put to record stream           */
  48. /****************************************************************************/
  49. {
  50.     int err=EOF;             /* return error (0=no error; !0=error) */
  51.  
  52.     if (!_rstatus(rp, R_WRITE)) {
  53.         rfldno(rp)++;
  54.         /* if not first field, put field separator */
  55.         if (rfldno(rp) > 1) {
  56.             err = _rputc(rp, rfldch(rp));
  57.             if (err) goto done;
  58.         }
  59.         /* put character */
  60.         err = _rputc(rp, ch);
  61.     }
  62. done:
  63.     return err;
  64. }
  65.  
  66. /****************************************************************************/
  67. int                          /* return 0 on success; !0 on error            */
  68.     rputs(                   /* put string to record stream                 */
  69.         REC  *rp,            /* record pointer                              */
  70.         char *str)           /* pointer to string                           */
  71. /****************************************************************************/
  72. /* note: assumes str does not contain a newline character */
  73. {
  74.     int err=EOF;             /* return error (0=no error; !0=error) */
  75.     size_t sl;               /* string length */
  76.  
  77.     if (!_rstatus(rp, R_WRITE)) {
  78.         rfldno(rp)++;
  79.  
  80.         /* if not first field, put field separator */
  81.         if (rfldno(rp) > 1) {
  82.             err = _rputc(rp, rfldch(rp));
  83.             if (err) goto done;
  84.         }
  85.  
  86.         /* if text delimiter not space, put leading text delimiter */
  87.         if (rtxtch(rp) != ' ') {
  88.             err = _rputc(rp, rtxtch(rp));
  89.             if (err) goto done;
  90.         }
  91.  
  92.         /* put string */
  93.         err = fputs(str, rfp(rp));
  94.         sl = strlen(str);
  95.         if (err==EOF) { 
  96.             rseterr(rp, R_ENOPUT);
  97.             goto done;
  98.         } else {
  99.             err = 0;
  100.             rcol(rp) += sl;
  101.         }
  102.  
  103.         /* if text delimiter not space, put trailing text delimiter */
  104.         if (rtxtch(rp) != ' ') {
  105.             err = _rputc(rp, rtxtch(rp));
  106.         }
  107.     }
  108. done:
  109.     return err;
  110. }
  111.